package com.qf.pubsub;

import com.qf.util.MQConnUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Publisher {

    public static final String EXCHANGE_NAME = "publish_subscribe_exchange";

    public static final String QUEUE_NAME1 = "publish_subscribe_queue1";

    public static final String QUEUE_NAME2 = "publish_subscribe_queue2";


    @Test
    public void publish() throws Exception, TimeoutException {
        //1. conn
        Connection conn = MQConnUtil.getConn();

        //2. chan...
        Channel channel = conn.createChannel();

        //3. 创建exchange     FANOUT - 直接和Queue进行绑定即可,routingKey必须写空串
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);

        //4. 创建queue
        channel.queueDeclare(QUEUE_NAME1,true,false,false,null);
        channel.queueDeclare(QUEUE_NAME2,true,false,false,null);

        //5. 将exchange和队列绑定到一起
        channel.queueBind(QUEUE_NAME1,EXCHANGE_NAME,"");
        channel.queueBind(QUEUE_NAME2,EXCHANGE_NAME,"");

        //6. 发送消息
        String msg = "publisher&subscribe!!";
        channel.basicPublish(EXCHANGE_NAME,"234234dfdfg",null,msg.getBytes());

        //7. 释放资源
        channel.close();
        conn.close();








    }


}
